package org.tigr.htc.server;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.util.HashMap;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.tigr.htc.cmd.ICommand;
import org.tigr.htc.common.HTCConfig;

/**
 * The <b>PersistLayer</b> class supplies methods to interact with the datastore.
 * This class implements the singleton pattern so that only a single instance is
 * created.
 */
public class PersistLayer {
    static Logger log = Logger.getLogger(PersistLayer.class);
    static final private PersistLayer pl = new PersistLayer();
    static private HashMap commands = new HashMap(400);
    static private HashMap runners = new HashMap(400);
    static private Properties conf = HTCConfig.getConfig();
    
    private PersistLayer( ) {  }
    
    /**
     * The method <code>getNextID</code> returns the next ID that can be used. The method
     * reads the current ID from the config class and increments the ID before returning the
     * current ID.
     * 
     * @return a <code>long</code> representing the next ID that can be used
     */
    public static synchronized long getNextID() {
        long id = Math.round(Math.random());
        try {
            id = Long.parseLong(conf.getProperty("id"));
            id++;
            conf.setProperty("id", Long.toString(id));
            HTCConfig.setConfig(conf);
        } catch(Exception e) {
            log.warn("IOException: missing property file " +  e.getMessage());
        }
        
        return id;
    }
    
    public static void persistCommand(ICommand cmd) {
        log.debug("In persistCommand");
        if (cmd.getID() == 0) {
            cmd.setID(PersistLayer.getNextID());
            log.warn("Command had an id of zero!!! Resetting id to " + cmd.getID());
        }
        commands.put(new Long(cmd.getID()), (Object)cmd);
    }
    
    public static void persistRunner(Long pId, IRunner runner) {
        log.debug("In persistRunner: " + pId);
        runners.put(pId, (Object) runner);
    }
    
    public static ICommand findCommand(Long id) {
        return (ICommand) commands.get(id);
    }
    
    public static IRunner findRunner(Long id) {
        log.debug("In findRunner: " + id);
        return (IRunner) runners.get(id);
    }
    
    public static void removeCommand(Long id) {
        log.debug("In removeCommand: " + id);
        commands.remove(id);
    }
    
    public static void removeRunner(Long id) {
        log.debug("In removeRunner: " + id);
        runners.remove(id);
    } 
    
    public static PersistLayer getInstance() {
        return pl;
    }
    
}
